import { gsap } from 'gsap'

export default class AnimationsModule {
    constructor() {
        this.animationsObject = {};
        this.resources = [];
        this.key = '';
    }
    add(key, value) {
        this.animationsObject[key] = value;
    }
    play(key, duration) {
        this.key = key;
        this.resources.push(
            gsap.timeline().to({}, {
                duration:duration < 1.25 ? 1.25 : duration, // 动画时长1.25秒
                onUpdate: () => {
                    console.log( this.animationsObject[key]);
                    
                    this.animationsObject[key].update(0.016); // 需要调用update触发渲染
                },
                onComplete: () => {
                    console.log("动画播放完成");
                }
            })
        )
    }
    reset(key) {
        if (!key) return;
        this.animationsObject[key]._actions.forEach(action => {
            this.resources.push(
                gsap.timeline().to(action, {
                    time: 0, // 反向速度
                    onUpdate: () => {
                        this.animationsObject[key].update(0); // 需要调用update触发渲染
                    },
                    onComplete: () => {
                        this.key = '';
                    }
                })
            )
        })
    }
} 